package com.yonyou.crm.bpub.template.rmimpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.yonyou.crm.bpub.template.rmitf.IPCTemplatesRmService;
import com.yonyou.crm.sys.modules.entity.BlockItemVO;
import com.yonyou.crm.sys.modules.entity.BlockVO;
import com.yonyou.crm.sys.modules.entity.LayoutVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutVO;
import com.yonyou.crm.sys.modules.service.BlockBizServiceImpl;
import com.yonyou.crm.sys.modules.service.BlockItemBizServiceImpl;
import com.yonyou.crm.sys.modules.service.LayoutBizServiceImpl;
import com.yonyou.crm.sys.modules.service.ListLayoutBizServiceImpl;

public class PCTemplateRmServiceImpl implements IPCTemplatesRmService{
	
	@Autowired
	private LayoutBizServiceImpl layoutService;
	@Autowired
	private BlockBizServiceImpl blockService;
	@Autowired
	private BlockItemBizServiceImpl blockItemService;
	@Autowired
	private ListLayoutBizServiceImpl listLayoutService;
	
	@Override
	public LayoutVO getLayout(Map<String, Object> param) {
		if(param == null) return null;
		Long biztypeId = null;
		if(!param.get("biztypeId").equals(null)){
			biztypeId = Long.parseLong(param.get("biztypeId").toString());
		}
		Integer clientType = null;
		if(!param.get("clientType").equals(null)){
			clientType = Integer.parseInt(param.get("clientType").toString());
		}
		Integer layoutType = null;
		if(!param.get("layoutType").equals(null)){
			layoutType = Integer.parseInt(param.get("layoutType").toString());
		}
		LayoutVO layOut = layoutService.getLayoutForCurrentUser(biztypeId, clientType, layoutType);
		if(layOut == null) return null;
		List<BlockVO> blocks = blockService.getBlockList(layOut.getId());
		if(blocks == null || blocks.size() == 0) return null;
		for(BlockVO block : blocks){
			List<BlockItemVO> blockItems = blockItemService.getBlockItemList(block.getId());
			if(blockItems != null && blockItems.size() > 0){
				block.setItems(blockItems);
			}
		}
		layOut.setMainLayout(blocks);
		return layOut;
	}

	@Override
	public ListLayoutVO getListLayout(Map<String, Object> param) {
		if(param == null) return null;
		Long objId = null;
		if(!param.get("module").equals(null)){
			objId = (Long)param.get("module");
		}
		Integer clientType = null;
		if(!param.get("clientType").equals(null)){
			clientType = (Integer)param.get("clientType");
		}
		ListLayoutVO listLayOut = listLayoutService.getListLayoutForCurrentUser(objId, clientType);
		return listLayOut;
	}

}
